package com.dzxcqmxm.Dao.impl;

import com.dzxcqmxm.Dao.Dao;
import com.dzxcqmxm.Dao.XtDao;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author Payboi
 * @create 2022/6/2-18:40
 * @FileName addXCDaoImpl
 * @Description
 */
public class XtDaoImpl implements XtDao {
    /**
     * 新增系统相册，
     * 返回Album对象
     * 发现返回不了，因为用不到，那就算了
     * @param XTXCName
     */
    @Override
    public void addXTXC(String XTXCName)throws Exception {
        File file = new File("D:/电子相册/"+XTXCName);
        if(file.mkdirs()){
            System.out.println(XTXCName+"创建成功！");
        }
        /**
         * 到这里我是已经创建成功了，然后我得把它写到数据库里,而且是插入语句就是说
         */
        com.dzxcqmxm.JB.Xt xt = new com.dzxcqmxm.JB.Xt();
        String sql = "insert into xt (XTXCName,XTXCUrl) values (?,?)";
        try {
            Connection connection = getConnection();
            PreparedStatement pstm = connection.prepareStatement(sql);
            //把系统相册名字和路径都写到数据库里
            pstm.setString(1, XTXCName);
            pstm.setString(2, "total/"+XTXCName);
            pstm.executeUpdate();
            Dao.close(null,pstm,connection);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 删除系统相册
     * @param XTXCName
     * @throws Exception
     */
    @Override
    public void delXTXC(String XTXCName) throws Exception {
        String sql = "DELETE FROM xt WHERE XTXCName =?";

        try {
            Connection connection = getConnection();
            PreparedStatement pstm = connection.prepareStatement(sql);

            //设置要删除的语句，这个获取的id是就是上面那个where id啦
            pstm.setString(1,XTXCName);
            //执行了删除语句
            pstm.executeUpdate();
            //关闭连接，防止卡住！
            Dao.close(null,pstm,connection);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败！！");

        }
    }

    /**
     * 查询所有系统相册
     * @return
     * @throws Exception
     */
    @Override
    public ArrayList<com.dzxcqmxm.JB.Xt> findAllXt() throws Exception {
        String sql4 = "select * from xt";
        ArrayList<com.dzxcqmxm.JB.Xt> xtArrayList = new ArrayList<com.dzxcqmxm.JB.Xt>();
        try {
            Connection connection = getConnection();
            PreparedStatement preparedStatement2 = connection.prepareStatement(sql4);
            //执行查询语句（赋值给结果集）
            ResultSet rSt = preparedStatement2.executeQuery();
            while (rSt.next()){
                /**
                 * 这里我只想获得系统相册名，其他先
                 */
                com.dzxcqmxm.JB.Xt xt = new com.dzxcqmxm.JB.Xt();

                xt.setXTXCName(rSt.getString("XTXCName"));
                xt.setXTXCUrl(rSt.getString("XTXCUrl"));

                xtArrayList.add(xt);
            }
            //关闭连接，防止卡住！
            Dao.close(rSt,preparedStatement2,connection);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询所有失败");
            return null;

        }
        return xtArrayList;
    }


}
